import { getExamList, getStudentOfExam, launchRequest, getRecheckResult } from '@/api/server/recheckRequest'
import { getRejectReason } from '@/api/server/recheckRequestManage'
import { ref } from 'vue'
import { useServerUserStore } from '@/stores/index'

export const useRecheckRequest = () => {
  const userStore = useServerUserStore()
  // 根据教师ID获取对应学科的考试列表
  const page = ref(1)
  const pageSize = ref(10)
  const examList = ref([])
  const total = ref(0)
  const timeOrder = ref('desc')

  const getExamListOfHook = async (params) => {
    const res = await getExamList(params)
    examList.value = res.data.data.list
    total.value = res.data.data.total
  }

  const handleReset = () => {
    page.value = 1
    pageSize.value = 10
    timeOrder.value = 'desc'
    getExamListOfHook({teacherId: userStore.userInfo.id, page: page.value, pageSize: pageSize.value,timeOrder: timeOrder.value})
  } 

  const handleQuery = async () => {
    await getExamListOfHook({
      teacherId: userStore.userInfo.id, 
      page: page.value, 
      pageSize: pageSize.value,
      timeOrder: timeOrder.value})
      
      ElMessage.success('查询成功')
  }

 


  //获取对应考试的自己班级的学生
  const studentList = ref([])
  const totalStudent = ref(0)
  const pageStudent = ref(1)
  const pageSizeStudent = ref(10)
  const studentAccount = ref('')
  const dialogVisibleOfStudent = ref(false)
  const examinationIdOfHook = ref('')
  const getStudentOfExamOfHook = async (examinationId) => {
    examinationIdOfHook.value = examinationId
    const res = await getStudentOfExam({
      examinationId: examinationId,
      teacherId: userStore.userInfo.id,
      studentAccount: studentAccount.value,
      page: pageStudent.value,
      pageSize: pageSizeStudent.value
    })
    studentList.value = res.data.data.list
    totalStudent.value = res.data.data.total
    dialogVisibleOfStudent.value = true
  }

  const handleResetOfStudent = () => {
    studentAccount.value = ''
    pageStudent.value = 1
    pageSizeStudent.value = 10
    getStudentOfExamOfHook(examinationIdOfHook.value)
  }
  
  const handleQueryOfStudent = async () => {
    if(!studentAccount.value){
      ElMessage.warning('请输入学号')
      return
    }
    await getStudentOfExamOfHook(examinationIdOfHook.value)
    if(studentList.value.length === 0){
      ElMessage.warning('没有找到该学生')
      return
    }
    ElMessage.success('查询成功')
  }


  // 申请查卷
  const studentPaperId = ref(0)
  const requestTeacherId = ref(0)
  const subjectLeaderId = ref(0)
  const studentQuestion = ref('')
  const teacherComment = ref('')
  const originalScore = ref(0)
  const recheckDialogVisible = ref(false)
  const openRecheckDialog = (row) => {
    recheckDialogVisible.value = true
    studentPaperId.value = row.studentPaperId
    requestTeacherId.value = userStore.userInfo.id
    originalScore.value = row.finalScore

    teacherComment.value = ''
    studentQuestion.value = ''

    console.log('studentPaperId: ',studentPaperId.value);
    console.log('requestTeacherId: ',requestTeacherId.value);
    console.log('originalScore: ',originalScore.value);
    
  }

  const handleRecheck = async () => {
    if(!studentQuestion.value){
      ElMessage.warning('请输入学生问题')
      return
    }
  
    await launchRequest({
      studentPaperId: studentPaperId.value,
      requestTeacherId: requestTeacherId.value,
      originalScore: originalScore.value,
      teacherComment: teacherComment.value,
      studentQuestion: studentQuestion.value
    })
    ElMessage.success('查卷申请成功')
    recheckDialogVisible.value = false
  }

  // 查看驳回原因
  const rejectReasonDialogVisible = ref(false)
  const rejectReason = ref('')
  const openRejectReasonDialog = async (row) => {
    const studentPaperId = row.studentPaperId
    const res = await getRejectReason({
      studentPaperId: studentPaperId
    })
    rejectReason.value = res.data.data
    rejectReasonDialogVisible.value = true
  }

  // 查看审查结果
  const recheckResultDialogVisible = ref(false)
  const recheckResult = ref({})
  const openRecheckResultDialog = async (row) => {
    const studentPaperId = row.studentPaperId
    const res = await getRecheckResult({
      studentPaperId: studentPaperId  
    })
    recheckResult.value = res.data.data
    recheckResultDialogVisible.value = true
  }

  return {
    examList,
    total,
    page,
    pageSize,
    timeOrder,
   
    handleReset,
    handleQuery,
    getExamListOfHook,


    studentList,
    totalStudent,
    pageStudent,
    pageSizeStudent,
    studentAccount,
    dialogVisibleOfStudent,
    getStudentOfExamOfHook,
    handleResetOfStudent,
    handleQueryOfStudent,

    // 查卷申请
    studentPaperId,
    requestTeacherId,
    subjectLeaderId,
    studentQuestion,
    teacherComment,
    originalScore,
    recheckDialogVisible,
    openRecheckDialog,
    handleRecheck,

    // 查看驳回原因
    rejectReasonDialogVisible,
    rejectReason,
    openRejectReasonDialog,

    // 查看审查结果
    recheckResultDialogVisible,
    recheckResult,
    openRecheckResultDialog
  }
} 
